package com.eagle.platform.api.controller;

import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.eagle.common.entity.po.RoleRResources;
import com.eagle.common.entity.po.query.AdminUserQuery;
import com.eagle.common.entity.po.query.RoleRResourcesQuery;
import com.eagle.common.exception.BusinessException;
import com.eagle.common.service.RoleRResourcesService;
import com.eagle.common.util.JacksonUtils;
import com.eagle.platform.api.constant.APIResultCodeEnum;
/**
 * 
 * 角色拥有资源对应表 控制层
 * 
 */
@Controller
@RequestMapping("roleRResources")
public class RoleRResourcesController extends BaseController{

	
	@Resource
	private RoleRResourcesService roleRResourcesService;
	
	/**
	 * 根据资源id查询角色或者根据角色id查询资源
	 */
	@RequestMapping(value = "list", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String list(@RequestBody(required = false) String id) {
		List<RoleRResources> roleRResources=null;
		RoleRResourcesQuery query = null;
		if(!StringUtils.isAllBlank(id)) {
			query=JacksonUtils.fromJson(id, RoleRResourcesQuery.class);
		}
		roleRResources=roleRResourcesService.findListByParam(query);
		if (query == null){
            query = new RoleRResourcesQuery();
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, roleRResources));
	}
	
	/**
	 * 添加角色拥有资源对应表信息
	 * param roleId (角色表主键)
	 * param resourceId (资源表主键)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "insert", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String insert(@RequestBody(required = false)RoleRResources resources
			) throws BusinessException {
		Integer query = 0;
		if( resources!=null) {
				roleRResourcesService.add(resources);
				query++;
			}
		if (query == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
	
	/**
	 * 修改角色拥有资源对应表信息
	 * param resourceId (角色拥有资源表)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "updateById", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String updateById(@RequestBody(required = false)RoleRResources resources) throws BusinessException {
		Integer query = 0;
		if( resources!=null ) {
			Integer roleId=resources.getRoleId();
			Integer resourceId=resources.getResourceId();
			roleRResourcesService.updateByRoleIdAndResourceId(resources, roleId, resourceId);
			query++;
		}
		if (query == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
	
	/**
	 * 删除角色拥有资源对应表信息
	 * param roleId (角色表主键)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "deleteroleById", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String deleteRoleById(@RequestBody(required = false)String roleId) throws BusinessException {
		Integer value = null;
		RoleRResourcesQuery query = null;
		if(roleId!=null) {
			query=JacksonUtils.fromJson(roleId, RoleRResourcesQuery.class);
			value=roleRResourcesService.deleteByRoleIdAndResourceId(query.getRoleId(), null);
		}
		if (value == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
}
